package org.jeecg.modules.innerApi.zhiYunApi.convert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.innerApi.common.responseDto.*;
import org.jeecg.modules.innerApi.zhiYunApi.util.ZhiyunApiResponse;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 将开票相关参数转换为新时代智云相关参数
 */
public class ZyResponseRtnConvert {
    /**
     * 将新时代智云开票结果转换为共享开票信息
     */
    public static InvoiceRtn skfpRtnConvert(ZhiyunApiResponse response){
        InvoiceRtn rtn=new InvoiceRtn();
        if(response.getCode()== 200){
            JSONObject jsonData=response.getData();
            InvoiceRtn.InvoiceRtnData rtnData=jsonData.toJavaObject(InvoiceRtn.InvoiceRtnData.class);
            try {
                String kqrq=jsonData.getString("kprq");
                Date kprq=DateUtils.parseDate(kqrq,"yyyyMMddHHmmss");
                rtnData.setKprq(kprq);
            }catch (Exception e) {
                throw new JeecgBootException(e.getMessage());
            }
            rtn.setCode("200");
            rtn.setSuccess(true);
            rtn.setMsg(response.getMsg());
            rtn.setData(rtnData);
        }else{
            rtn.setCode("500");
            rtn.setSuccess(false);
            rtn.setMsg(response.getMsg());
        }
        return rtn;
    }
    /**
     * 将新时代智云当前未开票发票号转换为共享未开票发票号
     */
    public static CurrentFphmRtn currentFphmRtnConvert(ZhiyunApiResponse response){
        CurrentFphmRtn rtn=new CurrentFphmRtn();
        if(response.getCode()== 200){
            JSONObject data=response.getData();
            CurrentFphmRtn.CurrentFphmRtnData rtnData=new CurrentFphmRtn.CurrentFphmRtnData();
            rtnData.setFpdm(data.getString("dqfpdm"));
            rtnData.setFphm(data.getString("dqfphm"));
            rtn.setSuccess(true);
            rtn.setMsg(response.getMsg());
            rtn.setData(rtnData);
        }else{
            rtn.setSuccess(false);
            rtn.setMsg(response.getMsg());
        }
        return rtn;
    }
    /**
     * 转换发票作废返回数据
     */
    public static FpzfRtn fpzfRtnConvert(ZhiyunApiResponse response){
        FpzfRtn rtn=new FpzfRtn();
        if(response.getCode()== 200){
            rtn.setSuccess(true);
            rtn.setMsg(response.getMsg());
            JSONObject data=response.getData();
            FpzfRtn.FpzfRtnData fpzfRtnData = data.toJavaObject(FpzfRtn.FpzfRtnData.class);
            try {
                fpzfRtnData.setZfrq(DateUtils.parseDate(data.getString("zfrq"),"yyyyMMddHHmmss"));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            rtn.setData(fpzfRtnData);
        }else{
            rtn.setSuccess(false);
            rtn.setMsg(response.getMsg());
        }
        return rtn;
    }

    /**
     * 转换红字申请数据返回数据
     */
    public static HzxxbsqRtn hzxxbsqRtnConvert(ZhiyunApiResponse response){
        HzxxbsqRtn rtn=new HzxxbsqRtn();
        if(response.getCode()== 200){
            rtn.setSuccess(true);
            rtn.setMsg(response.getMsg());
        }else{
            rtn.setSuccess(false);
            rtn.setMsg(response.getMsg());
        }
        return rtn;
    }

    /**
     * 转换红字同步数据返回数据
     */
    public static HzxxbtbRtn hzxxbtbRtnConvert(String response){
        HzxxbtbRtn rtn=JSONObject.parseObject(response,HzxxbtbRtn.class);
        if(rtn==null||rtn.getCode()==null){
            rtn.setSuccess(false);
            rtn.setMsg("税控调用异常："+response);
        }else {
            if (rtn.getCode().equals("200")) {
                rtn.setSuccess(true);
            } else {
                rtn.setSuccess(false);
            }
        }
        return rtn;
    }

    /**
     * 智能赋码数据返回数据
     */
    public static Result<List<ZnfmRtn>> znfmRtnConvert(ZhiyunApiResponse rtn){
        List<ZnfmRtn> znfmRtns=null;
        if (rtn!=null&&rtn.getCode()==200) {
            JSONObject data=rtn.getData();
            if(data!=null&&data.containsKey("groups")){
                JSONObject groups=data.getJSONObject("groups");
                if(groups!=null&&groups.containsKey("group")){
                    JSONObject group=groups.getJSONObject("group");
                    if(group!=null&&group.containsKey("codes")){
                        String spmc=group.getString("spmc");
                        JSONObject codes=group.getJSONObject("codes");
                        JSONArray codesJSONArray=codes.getJSONArray("code");
                        znfmRtns=new ArrayList<>();
                        for(int i=0;i<codesJSONArray.size();i++){
                            String fmcode=codesJSONArray.getString(i);
                            String[] fmc=fmcode.split("--");
                            ZnfmRtn rtnfmjg=new ZnfmRtn();
                            rtnfmjg.setPercent(fmc[2]);//匹配率
                            rtnfmjg.setSpmc(spmc);//商品名称
                            rtnfmjg.setCode(fmc[1]);//税收分类编码
                            rtnfmjg.setSsflmc(fmc[0]);//税收分类名称
                            rtnfmjg.setDesc("");//描述
                            znfmRtns.add(rtnfmjg);
                        }
                    }
                }
            }
            return Result.OK("赋码成功",znfmRtns);
        }else{
           return Result.error(rtn.getMsg());
        }
    }

    /**
     * 转换红字撤销数据返回数据
     */
    public static CommonRtn commonRtnConvert(ZhiyunApiResponse response){
        CommonRtn rtn=new CommonRtn();
        if(response.getCode()== 200){
            rtn.setSuccess(true);
            rtn.setMsg(response.getMsg());
        }else{
            rtn.setSuccess(false);
            rtn.setMsg(response.getMsg());
        }
        return rtn;
    }


    public static InvoiceQueryRtn invoiceQueryRtnConvert(String result){
        JSONObject jsonObject = (JSONObject) JSON.parse(result);
        InvoiceQueryRtn rtn=new InvoiceQueryRtn();
        if(jsonObject.getIntValue("code")== 200){
            rtn.setCode("200");
            rtn.setSuccess(true);
            rtn.setMsg(jsonObject.getString("msg"));

            rtn.setTotal(jsonObject.getIntValue("total"));

            JSONArray data = jsonObject.getJSONArray("data");
            if(data != null) {
                 JSONObject s = (JSONObject) data.get(0);
                 JSONArray listData = s.getJSONArray("listData");
                 JSONObject price = s.getJSONObject("price");
                 rtn.setDataPrice(JSON.toJavaObject(price, InvoiceQueryRtn.InvoiceQueryRtnDataPrice.class));
                 rtn.setData(listData.toJavaList(InvoiceQueryRtn.InvoiceQueryRtnData.class));
            }
        }else{
            rtn.setCode("500");
            rtn.setSuccess(false);
            rtn.setMsg(jsonObject.getString("msg"));
        }
        return rtn;
    }
}
